﻿using System.Windows.Controls;
using Chaotic.Module.DBTool.ViewModels;
using Telerik.Windows.Controls;

namespace Chaotic.Module.DBTool.Items
{
    /// <summary>
    /// DbConnectionControl.xaml 的交互逻辑
    /// </summary>
    public partial class DbConnectionControl : UserControl
    {
        public DbConnectionControl()
        {
            InitializeComponent();
            filePathpicker.DialogOpening += FilePathpicker_DialogOpening;
        }

        private void FilePathpicker_DialogOpening(object? sender, Telerik.Windows.Controls.FileDialogs.DialogOpeningEventArgs e)
        {
            if (e.Dialog is RadOpenFileDialog radOpenFileDialog)
            {
                if (this.DataContext is RegistDBViewModel { FileDataSource: not null } viewModel)
                {
                    if (viewModel.FileDataSource.ConnectType == DataBase.DataAccess.DatabaseType.OleDBSupported)
                        radOpenFileDialog.Filter = "excel 2003 （*.xls)|*.xls|excel 2007/2010 (*.xlsx)|*.xlsx";
                    else if (viewModel.FileDataSource.ConnectType == DataBase.DataAccess.DatabaseType.Sqlite)
                    {
                        radOpenFileDialog.Filter = "sqlite （*.db)|*.db";
                    }
                }
            }
        }

        private void RadTabControl_SelectionChanged(object sender, RadSelectionChangedEventArgs e)
        {
            if(this.DataContext is RegistDBViewModel viewModel && e.AddedItems.Count > 0)
            {
                if (e.AddedItems[0] is RadTabItem tabItem)
                {
                    viewModel.TabHeader = tabItem.Header.ToString();
                }
            }
        }
    }
}
